package com.jiusit.page;

import java.io.IOException;
import java.sql.ResultSet;

import com.jiusit.dao.BankPageDao;
import com.jiusit.dao.DataDao;
import com.webbuilder.utils.*;

import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class bankPage {
	public List<Object> bankTradingPage(HttpServletRequest request,
			HttpServletResponse response,String ID) throws ServletException, IOException{
		try {
		//每页显示记录数
		 int PageSize = 10; //每页显示记录数
		 int StartRow = 0; //开始显示记录的编号
		 int PageNo=0;//需要显示的页数
		 int CounterStart=0;//每页页码的初始值
		 int CounterEnd=0;//显示页码的最大值
		 int RecordCount=0;//总记录数;
		 int MaxPage=0;//总页数
		 int PrevStart=0;//前一页
		 int NextPage=0;//下一页
		 int LastRec=0;
		 int LastStartRecord=0;//最后一页开始显示记录的编号

		 //获取需要显示的页数，由用户提交
		 if(request.getParameter("PageNo")==null){ //如果为空，则表示第1页
		   if(StartRow == 0){
		      PageNo = StartRow + 1; //设定为1
		   }
		 }else{
		   PageNo = Integer.parseInt(request.getParameter("PageNo")); //获得用户提交的页数
		   StartRow = (PageNo - 1) * PageSize; //获得开始显示的记录编号
		 }

		 //因为显示页码的数量是动态变化的，假如总共有一百页，则不可能同时显示100个链接。而是根据当前的页数显示
		 //一定数量的页面链接

		 //设置显示页码的初始值!!
		 if(PageNo % PageSize == 0){
		   CounterStart = PageNo - (PageSize - 1);
		 }else{
		   CounterStart = PageNo - (PageNo % PageSize) + 1;
		 }
		 CounterEnd = CounterStart + (PageSize - 1);
		 
		 //获取总记录数
		 String rsSql="select count(*) from bank_trading_records where bank_users_id = '"+ID+"'";
		 ResultSet rs;
		 rs = DbUtil.query(request,rsSql);
		 rs.next();
		 RecordCount = rs.getInt(1);

		 //rs = statement.executeQuery("SELECT usercode,username,password,comcode,flag_level,flag_status FROM zyd_user ORDER BY usercode DESC LIMIT "+StartRow+", "+PageSize);
		 rsSql = "select date(t.trading_date) trading_date,t.spending_money,t.deposit_money,t.account_balance,t1.key_value register_institutions,t.opposite_side_account,t.opposite_side_name,t.bank_remarks from bank_trading_records t "+
		                  "left join (select key_name,key_value  from wb_key where key_type like '%%' and key_pid = '24YL2ST8D1RL' and key_is_leaf=1) t1 on t.register_institutions=t1.key_name "+
		                  "where bank_users_id = '"+ID+"' order by t.trading_date desc LIMIT "+StartRow+", "+PageSize;
		 rs = DbUtil.query(request,rsSql);


		 //获取总页数
		 MaxPage = RecordCount % PageSize;
		 if(RecordCount % PageSize == 0){
		   MaxPage = RecordCount / PageSize;
		 }else{
		    MaxPage = RecordCount/PageSize+1;
		 }
		 //将分页结果存入DAO
		 BankPageDao bankPageDao=new BankPageDao(PageSize, StartRow, PageNo, CounterStart,
					CounterEnd, RecordCount, MaxPage, PrevStart,
					NextPage, LastRec, LastStartRecord);
		 DataDao data=new DataDao(rs);
		 List<Object> list=new ArrayList<Object>();
		 list.add(bankPageDao);
		 list.add(data);
		 return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
